<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dept">
      <div class="tj">
        <div class="item">
          <p>可用余额 (元)</p>
          <p>￥{{ (info && info.walletAmount) || 0 }}</p>
        </div>
        <div class="item">
          <p>提现中 (元)</p>
          <p>￥{{ (info && info.withdrawingAmount) || 0 }}</p>
        </div>
        <!-- <div class="item">
          <p>待入账 (元)</p>
          <p>￥{{ (info && info.walletAmount) || 0 }}</p>
        </div> -->
      </div>
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="
          getDataList();
          getInfo();
        "
      >
        <el-form-item v-if="showSelect">
          <el-select v-model="dataForm.merchantId" placeholder="请选择商家名称">
            <el-option
              v-for="item in merchantNameOption"
              :key="item.merchantId"
              :label="item.merchantName"
              :value="item.merchantId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="dataForm.consumeType"
            placeholder="消费类型"
            clearable
          >
            <el-option
              :value="item.val"
              v-for="item in type1"
              :key="item.val"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="dataForm.flowType"
            placeholder="资金流向"
            clearable
          >
            <el-option
              :value="item.val"
              v-for="item in type2"
              :key="item.val"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-select
            v-model="dataForm.payType"
            placeholder="支付方式"
            clearable
          >
            <el-option
              :value="item.val"
              v-for="item in type3"
              :key="item.val"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-input
            v-model="dataForm.orderCode"
            placeholder="订单编号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(), getInfo()">{{
            $t("query")
          }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="tixian()">提现</el-button>
        </el-form-item>
         <el-form-item v-if="isExport">
          <el-button type="primary" @click="exportHandle()">导出</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="发生时间"
          sortable="custom"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="merchantName"
          label="名称"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="orderCode"
          label="订单编号"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="amount"
          label="实付金额"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="orderAmount"
          label="原价"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="discountAmount"
          label="优惠券金额"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="consumeType"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          label="消费类型"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.consumeType == 1">下单</span>
            <span v-if="scope.row.consumeType == 2">退款</span>
            <span v-if="scope.row.consumeType == 3">提现</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="flowType"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          label="资金流向"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.flowType == 1">收入</span>
            <span v-if="scope.row.flowType == 2">支出</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="payType"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          label="支付方式"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.payType == 1">微信</span>
            <span v-if="scope.row.payType == 2">支付宝</span>
            <span v-if="scope.row.payType == 3">云闪付</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
                </template>
            </el-table-column> -->
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: `/${this.type}/wallet/info/flowPage`,
        getDataListIsPage: true,
        createdIsNeed: false,
        exportURL: `/${this.type}/wallet/info/export`
      },
      value1: [],
      type1: [
        {
          val: 1,
          label: "下单",
        },
        {
          val: 2,
          label: "退款",
        },
        {
          val: 3,
          label: "提现",
        },
      ],
      type2: [
        {
          val: 1,
          label: "收入",
        },
        {
          val: 2,
          label: "支出",
        },
      ],
      type3: [
        {
          val: 1,
          label: "微信",
        },
        {
          val: 2,
          label: "支付宝",
        },
        {
          val: 3,
          label: "云闪付",
        },
      ],
      info: {},
      merchantNameOption: [],
       showSelect:false,
      dataForm: {
        consumeType: "",
        startDate: "",
        endDate: "",
        flowType: "",
        merchantId: localStorage.getItem("MerchantId"),
        orderCode: "",
        payType: "",
        merchantType:""
      },
    };
  },
  watch: {
    value1(newval, oldval) {
      if (newval != oldval && newval) {
        this.dataForm.startDate = newval[0];
        this.dataForm.endDate = newval[1];
      } else {
        this.dataForm.startDate = "";
        this.dataForm.endDate = "";
      }
    },
  },
  components: {},
  props: ["merchantType", "merchantId", "type",'isExport'],

  methods: {
    getmerchantNameOption() {
      this.$http
        .post("/order/merchant/wallet/info/getMerchantInfoList2", {
          merchantTypeList: this.merchantType,
        })
        .then(({ data: res }) => {
          if (res.msg == "success") {
            this.merchantNameOption = res.data;
            if (!this.merchantId) {
              this.dataForm.merchantId = res.data[0].merchantId;
            }
            this.getDataList();
            this.getInfo();
          }
        });
    },
    init() {
      this.dataForm.merchantId = this.merchantId;
      this.dataForm.merchantType = this.merchantType.join(',')
      console.log(this.$store.state.user.typelist)
      if (this.$store.state.user.typelist.indexOf("3") > -1) {
        this.getmerchantNameOption();
        this.showSelect=true;
      } else {
        this.getDataList();
        this.getInfo();
      }
    },
    tixian() {
      let merchantType = localStorage.getItem('ss_merchantCategory');
      if (this.merchantNameOption.length>0) {
        merchantType = this.merchantNameOption.filter(
          (item) => item.merchantId == this.dataForm.merchantId
        )[0].merchantType;
      }
      this.$prompt("请输入提现金额", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /[\d.]/,
        inputErrorMessage: "提现金额只能输入数字",
      })
        .then(({ value }) => {
          if (value > this.info.walletAmount) {
            return this.$message.error("商家账户余额不足，不支持提现!");
          }
          this.$http
            .post("/order/ismerchantwithdrawalcheckinfo", {
              merchantId: this.dataForm.merchantId,
              merchantType: merchantType,
              amount: value,
            })
            .then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  type: "success",
                  message: "操作成功",
                });
              } else {
                return this.$message.error(res.data.msg);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    getInfo() {
      this.$http
        .get(`/${this.type}/wallet/info/getMerchantInfoById`, {
          params: {
            merchantType: this.merchantType.join(','),
            merchantId: this.dataForm.merchantId,
          },
        })
        .then((res) => {
          console.log(res);
          if (res.data.code == 0) {
            this.info = res.data.data;
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.tj {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 10px solid #eee;
  width: 108%;
  margin-left: -4%;

  .item {
    p {
      color: #666;
      margin: 0;

      &:last-child {
        font-weight: bold;
        font-size: 20px;
        margin-top: 12px;
        color: #333;
      }
    }
  }
}
</style>
